<template>
  <div class="dashboard-container">
    <el-card style="height: 120px; margin-bottom: 20px">
      <!-- 给el-form绑定form -->
      <el-form label-width="80px" :model="form" :inline="true">
        <el-form-item label="产品分类">
          <el-select v-model="form.state" placeholder="产品分类">
            <el-option
              v-for="a in reqCategory2"
              :key="a.id"
              :label="a.name"
              :value="a.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.typ" placeholder="类型">
            <el-option
              v-for="a in reqCategory1"
              :key="a.id"
              :label="a.name"
              :value="a.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchclick"
          style="margin-left: 12px"
          >筛选</el-button
        >
        <el-button @click="resetSearch">重置</el-button>
        <el-button @click="addrenyuan" type="success">添加</el-button>
      </el-form>
    </el-card>
    <el-table
      :data="
        tableData.slice(
          (form.pageNo - 1) * form.pageSize,
          form.pageNo * form.pageSize
        )
      "
      border
    >
      <el-table-column prop="id" label="ID" align="center"> </el-table-column>
      <el-table-column prop="name" label="vip套餐名称" align="center">
      </el-table-column>
      <el-table-column prop="price" label="价格" align="center">
      </el-table-column>
      <el-table-column prop="originalprice" label="原价" align="center">
      </el-table-column>
      <el-table-column prop="day" label="多少天" align="center">
      </el-table-column>
      <el-table-column prop="num" label="使用次数" align="center">
      </el-table-column>
      <el-table-column prop="weak" label="到期前几天弱提醒" align="center">
      </el-table-column>
      <el-table-column prop="strong" label="到期前几天强提醒" align="center">
      </el-table-column>
      <el-table-column prop="产品分类" label="产品分类" align="center">
        <template slot-scope="{ row }">
          <el-tag :type="row.state == 1 ? 'primary' : 'success'">{{
              row.state | statefilter
            }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="类型" label="类型" align="center">
        <template slot-scope="{ row }">
          <el-tag :type="row.typ == 1 ? 'primary' : 'success'">{{
              row.typ | statefilter1
            }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="changerenyuan(scope.row)"
            >修改</el-button
          >
          <el-button type="danger" @click="deldel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :currentPage="this.form.pageNo"
      :size="this.form.pageSize"
      :total="total"
      @changelimit="changelimit"
      @changecurrentPage="changecurrentPage"
    ></Pagination>
    <add ref="add" @refresh="refresh"></add>
  </div>
</template>

<script>
import { getvippackagelist, delvippackagedel } from "@/api/vippackage";
import add from "./modules/add";
export default {
  name: "Dashboard",
  components: {
    add,
  },
  data() {
    return {
      that: this,
      reqCategory2: [
        { id: 0, name: "全部" },
        { id: 1, name: "基础产品" },
        { id: 2, name: "收款" },
        { id: 3, name: "优惠卷" },
        { id: 4, name: "充值购" },
        { id: 5, name: "单品快卖" },
        { id: 6, name: "商城" },
        { id: 7, name: "会员系统" },
        { id: 8, name: "通用套餐" },
      ],
      reqCategory1: [
        { id: 0, name: "全部" },
        { id: 1, name: "落地页" },
        { id: 2, name: "正常套餐" },
      ],
      form: {
        state: 0,
        typ: 0,
        pageNo: 1,
        pageSize: 5,
      },
      total: 0,
      tableData: [],
    };
  },
  filters: {
    statefilter(val) {
      if (val == 1) {
        return "基础产品";
      }else if (val == 2) {
        return "收款";
      }else if (val == 3) {
        return "优惠卷";
      }else if (val == 4) {
        return "充值购";
      }else  if (val == 5) {
        return "单品快卖";
      }else  if (val == 6) {
        return "商城";
      }else  if (val == 7) {
        return "会员系统";
      }else  if (val == 8) {
        return "通用套餐";
      }
    },
    statefilter1(val) {
      if (val == 1) {
        return "落地页";
      }else if (val == 2) {
        return "正常套餐";
      }
    },
    datefilter(val) {
      if (val) {
        var date = new Date(parseInt(val) * 1000);
        var Year = date.getFullYear();
        var Moth =
          date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1;
        var Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var Hour =
          date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var Minute =
          date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var Sechond =
          date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var GMT = Year + "-" + Moth + "-" + Day;

        return GMT;
      } else {
        return "";
      }
    },
  },
  methods: {
    getSimpleText(html) {
      var re1 = new RegExp("<.+?>", "g"); //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
      var msg = html.replace(re1, ""); //执行替换成空字符
      return msg;
    },
    searchclick() {
      this.getcouponlist1();
    },
    resetSearch() {
      this.form.state = 0;
      this.form.typ = 0;
      this.getcouponlist1();
    },
    addrenyuan() {
      this.$refs.add.add();
    },
    // 子传父:自定义事件
    refresh() {
      this.getcouponlist1();
    },
    async getcouponlist1() {
      let result = await getvippackagelist({
        typ: this.form.typ,
        state: this.form.state,
        pageNo: this.form.pageNo,
        pageSize: this.form.pageSize,
      });
      console.log(result);
      if (result.code == 0) {
        if (result.data.data != []) {
          this.tableData = result.data.data;
          this.total = result.data.length;
        }
      }
    },
    changelimit(val) {
      this.form.pageSize = val;
      this.getcouponlist1();
    },
    changecurrentPage(val) {
      this.form.pageNo = val;
      this.getcouponlist1();
    },
    changerenyuan(val) {
      this.$refs.add.edit(val);
    },
    async deldel(val) {
      let result = await delvippackagedel({ id: val.id });
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "删除成功",
          type: "success",
        });
        this.getcouponlist1();
      }
    },
  },
  watch: {
    "form.name": {
      handler(newVal) {
        if (newVal == "") {
          this.getcouponlist1();
        }
      },
    },
  },
  mounted() {
    this.getcouponlist1();
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
